import { setGlobal } from '@swda/utils';


function Window_PetInfo() {
    this.initialize.apply(this, arguments);
}

Window_PetInfo.prototype = Object.create(Window_Base.prototype);
Window_PetInfo.prototype.constructor = Window_PetInfo;

Window_PetInfo.prototype.update = function() {
    Window_Base.prototype.update .call(this);
};

Window_PetInfo.prototype.initialize = function(x,y,w,h) {
    Window_Base.prototype.initialize.call(this, x,y,w,h);
    this._enemy = null;
    this.opacity = 0;
    this.refresh();
};

Window_PetInfo.prototype.setEnemy = function(item) {
    if(!item.enemyId || item.enemyId<=0){
        this._enemy = null;
    }else{
        const enemy = $dataEnemies[item.enemyId]
        if (this._enemy !== enemy) {
            this._enemy = enemy;
        }
    }
    this.refresh();
};

Window_PetInfo.prototype.enemy = function(item) {
    return new Game_Enemy(this._enemy.id, 0, 0);
};
Window_PetInfo.prototype.showInfo = function(item) {
    this.setEnemy(item);
    this.show()
}

Window_PetInfo.prototype.refresh = function() {
    this.contents.clear();
    if (!!this._enemy) {
        if($gameParty.inBattle()) this.drawBackground();
        this.drawBlock1();    // 核心数值与基础数值
        this.drawBlock2();    // 属性抗性
        this.drawBlockSkill();// 奇术
    }
};

Window_PetInfo.prototype.blockWidth = function(y) {
    return 148;
};
Window_PetInfo.prototype.previewWidth = function(y) {
    return 192 + 48;
};
Window_PetInfo.prototype.previewHeight = function(y) {
    return 280 + 48;
};

// 画背景
Window_PetInfo.prototype.drawBackground = function() {
    let rect = new Rectangle();
    rect.x = 0;
    rect.y = 0;
    rect.width = this.width - 2 * this.padding;
    rect.height = this.height - 2 * this.padding;

    const color = 'rgba(0, 0, 0, 0.3)';
	const padding = 4;
	this.contents.fillRect(rect.x, rect.y, rect.width, rect.height, color);
	this.contents.fillRect(rect.x + padding, rect.y + padding, rect.width - padding * 2, rect.height - padding * 2, color);
}
// 核心数值（命仙体）及基础数值（物攻、仙攻等）
Window_PetInfo.prototype.drawBlock1 = function() {
    const lineHeight = this.smallerLineHeight();
    const dx = this.previewWidth() + 24;
    let y = lineHeight * 0.5;

    this.setSmallerFont();
    this.changeTextColor(this.textColor(6));
    this.drawText(TextManager.hp, dx, y);
    this.changeTextColor(this.textColor(0));
    this.drawText(this.enemy().hp, dx + 96, y);
    y += lineHeight;

    this.setSmallerFont();
    this.changeTextColor(this.textColor(6));
    this.drawText(TextManager.mp, dx, y);
    this.changeTextColor(this.textColor(0));
    this.drawText(this.enemy().mp, dx + 96, y);
    y += lineHeight;

    this.changeTextColor(this.textColor(6));
    this.drawText(TextManager.tp, dx, y);
    this.changeTextColor(this.textColor(0));
    this.drawText(this.enemy().tp, dx + 96, y);
    y += lineHeight;

    // 基础数值
    this.changeTextColor(this.textColor(6));
    this.drawText(TextManager.param(2), dx, y);
    this.changeTextColor(this.textColor(0));
    this.drawText(this.enemy().atk, dx + 96, y);
    y += lineHeight;

    this.changeTextColor(this.textColor(6));
    this.drawText(TextManager.param(3), dx, y);
    this.changeTextColor(this.textColor(0));
    this.drawText(this.enemy().def, dx + 96, y);
    y += lineHeight;


    this.changeTextColor(this.textColor(6));
    this.drawText(TextManager.param(4), dx, y);
    this.changeTextColor(this.textColor(0));
    this.drawText(this.enemy().mat, dx + 96, y);
    y += lineHeight;

    this.changeTextColor(this.textColor(6));
    this.drawText(TextManager.param(5), dx, y);
    this.changeTextColor(this.textColor(0));
    this.drawText(this.enemy().mdf, dx + 96, y);
    y += lineHeight;


    this.changeTextColor(this.textColor(6));
    this.drawText(TextManager.param(6), dx, y);
    this.changeTextColor(this.textColor(0));
    this.drawText(this.enemy().agi, dx + 96, y);
    //y += lineHeight;

   /* 
    this.changeTextColor(this.textColor(6));
    this.drawText(TextManager.param(7), dx, y);
    this.resetTextColor();
    this.drawText(this.enemy().luk, dx + 96, y);
    */
    this.resetFontSettings();

};

// 显示属性抗性和攻击属性
Window_PetInfo.prototype.drawBlock2 = function() {
    const lineHeight = this.smallerLineHeight();
    const dx = 48;
    let y = this.previewHeight() + lineHeight * 0.5;
    this.setSmallerFont();
    this.changeTextColor(this.textColor(6));
    this.drawText("攻击附加", dx, y);
    this.changeTextColor(this.textColor(0));
    this.drawStates(this.enemy().attackStates(), dx + 128, y);

    y +=lineHeight;
    const infoArray = [1, 2, 3];
    for (var i = 0; i < infoArray.length; ++i) {
        this.drawElementData(infoArray[i], dx, y);
        y += lineHeight;
    }
    this.resetFontSettings();
};

Window_PetInfo.prototype.drawStates = function(states, x, y){
    const length = states.length;
    const d = 24;
    let total = 0;
    for(let i = 0; i < length; i++){
        let stateiconId = $dataStates[states[i]].iconIndex;
        if(stateiconId > 0){
            this.drawTextEx('\\i['+ stateiconId +']', x-6, y);
            x += d;
            total++;
        }
    }
    if(total==0)
        this.drawText('无', x, y);
}

Window_PetInfo.prototype.drawElementData = function (eleId, dx, dy, dw) {
    var eleName = $dataSystem.elements[eleId];
    var eleRate = (this.enemy().elementRate(eleId));

    this.setSmallerFont();
    this.changeTextColor(this.textColor(6));
    this.drawText(eleName, dx, dy);
    this.changeTextColor(this.textColor(0));

    var eleText = '';
    if (eleRate > 1) {
        eleText = '损 ';
        eleRate -= 1;
        this.changeTextColor('#ff4d00');
    
    } else if (eleRate == 1) {
        eleText = '—';
        this.changeTextColor('#fbdb45');

    } else if (eleRate >= 0) {
        eleText = '抗 ';
        eleRate -= 1;
        this.changeTextColor('#59ba8e');
    } else {
        eleText = '吸 ';
        this.changeTextColor('#65cbff');
    }
    /*
    if (eleRate == 1) {
        var text = eleText;
    } else {
        var text = eleText + Math.abs(Math.round(eleRate * 100)).toFixed(Yanfly.Param.StatusEleDec) + '%';
    }*/
    var text = eleText;
    this.drawText(text, dx + 128, dy, dw );
        

    this.resetFontSettings();

};


// 技能列表
Window_PetInfo.prototype.drawBlockSkill = function() {
    const lineHeight = this.smallerLineHeight();
    const dx = this.previewWidth() + 24;
    let y = this.previewHeight() - 2 * lineHeight;
    this.setSmallerFont();
    this.changeTextColor(this.textColor(6));
    this.drawText(TextManager.skill, dx, y);
    this.changeTextColor(this.textColor(0));
    y += lineHeight * 1.5;
    
    let skillCount = 0;
    for (var i = 0; i < this._enemy.actions.length; i++) {
        if (this._enemy.actions[i].skillId>6){
            var skill = $dataSkills[this._enemy.actions[i].skillId];
            if (!DataManager.isAttack(skill)){
                this.drawItemName(skill, dx, y );
                skillCount++;
                y += lineHeight;
            }
        }
        
    }

    if (skillCount <= 0){
		this.drawText('无', dx, y);
	}
    this.resetFontSettings();
};

Window_PetInfo.prototype.setItemTextColor = function() {
    this._resetTextColor = 0;
};

setGlobal('Window_PetInfo', Window_PetInfo)